package com.dhcc.bpm.modules.gateway.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.gateway.entity.GatewayRoute;
import com.dhcc.bpm.modules.gateway.service.IGatewayRouteService;
import com.dhcc.bpm.modules.gateway.vo.GatewayRouteVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * @ClassName GatewayRouteController
 * @Description 网关路由表 控制器
 * @Author YaoZC
 * @Date 2019/11/26 16:08
 * @Version 1.0
 * @Copyright: 2019 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-gateway/gatewayroute")
@Api(value = "", tags = "接口")
public class GatewayRouteController extends BladeController {

	private IGatewayRouteService gatewayRouteService;

	/**
	 * @Description 查看网关路由详情
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.gateway.entity.GatewayRoute>
	 **/
	@ApiLog("查看网关路由详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gatewayRoute")
	@Log(title = "查看网关路由详情",businessType = BusinessType.SELECT)
	public R<GatewayRoute> detail(GatewayRoute gatewayRoute) {
		GatewayRoute detail = gatewayRouteService.getOne(Condition.getQueryWrapper(gatewayRoute));
		if(detail != null){
			String [] routeConfig =  detail.getRouteConfig().replace("[", "").replace("]", "").replaceAll("},","}@@").split("@@");
			List<String> route = new ArrayList<>();
			for(int i = 0; i< routeConfig.length; i++){
				System.out.println(routeConfig[i]);
				JSONObject json = JSONObject.parseObject(routeConfig[i].replace("[", "").replace("]",""));
				if("After".equals(json.get("name"))){
					JSONObject config = new JSONObject();
					config.put("name", "Between");
					config.put("predicateValue", json.get("predicateValue") + ",");
					route.add(config.toJSONString());
					config = null;
				}else if("Before".equals(json.get("name"))){
					JSONObject config = new JSONObject();
					config.put("name", "Between");
					config.put("predicateValue",  "," + json.get("predicateValue"));
					route.add(config.toJSONString());
					config = null;
				}else{
					route.add(routeConfig[i]);
				}
			}
			detail.setRouteConfig(route.toString());
		}
		return R.data(detail);
	}

	/**
	 * @Description 查看网关路由分页列表
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @param query
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.gateway.entity.GatewayRoute>>
	 **/
	@ApiLog("查看网关路由分页列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "opcGatewayId", value = "网关ID", paramType = "query", dataType = "Long"),
	})
	@ApiOperation(value = "分页", notes = "传入gatewayRoute")
	@Log(title = "查看网关路由分页列表",businessType = BusinessType.SELECT)
	public R<IPage<GatewayRoute>> list(@ApiIgnore @RequestParam Map<String, Object> gatewayRoute, Query query, BladeUser bladeUser) {
		//IPage<GatewayRoute> pages = gatewayRouteService.page(Condition.getPage(query), Condition.getQueryWrapper(gatewayRoute));
		LambdaQueryWrapper<GatewayRoute> queryWrapper = Condition.getQueryWrapper(gatewayRoute, GatewayRoute.class).lambda().orderByDesc(GatewayRoute::getCreateTime);
		IPage<GatewayRoute> pages = gatewayRouteService.page(Condition.getPage(query), (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.eq(GatewayRoute::getTenantId, bladeUser.getTenantId()) : queryWrapper);
		return R.data(pages);
	}

	/**
	 * @Description 查看网关路由自定义分页列表
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.gateway.vo.GatewayRouteVO>>
	 **/
	@ApiLog("查看网关路由分页列表")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayRoute")
	@Log(title = "查看网关路由自定义分页列表",businessType = BusinessType.SELECT)
	public R<IPage<GatewayRouteVO>> page(GatewayRouteVO gatewayRoute, Query query) {
		IPage<GatewayRouteVO> pages = gatewayRouteService.selectGatewayRoutePage(Condition.getPage(query), gatewayRoute);
		return R.data(pages);
	}

	/**
	 * @Description 网关路由新增
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关路由新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入gatewayRoute")
	@Log(title = "网关路由新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody GatewayRoute gatewayRoute) {
		return R.status(gatewayRouteService.save(gatewayRoute));
	}

	/**
	 * @Description 网关路由修改
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关路由修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入gatewayRoute")
	@Log(title = "网关路由修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody GatewayRoute gatewayRoute) {
		return R.status(gatewayRouteService.updateById(gatewayRoute));
	}

	/**
	 * @Description 网关路由新增或修改
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关路由新增或修改")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "网关路由新增或修改", notes = "传入gatewayRoute")
	@Log(title = "网关路由新增或修改", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody GatewayRoute gatewayRoute) {
		gatewayRoute.setFilters(gatewayRoute.getFilters().replace("&amp;", "&"));
		return R.status(gatewayRouteService.saveOrUpdate(gatewayRoute));
	}


	/**
	 * @Description 网关路由删除
	 * @Date 2019/11/26 16:08
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关路由删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "网关路由删除", notes = "传入ids")
	@Log(title = "网关路由删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(gatewayRouteService.deleteLogic(Func.toLongList(ids)));
	}

	public R getGatewayRoute(Long id){

		return R.data(gatewayRouteService.getByGatewayId(id));
	}
	/**
	 * @Description 根据基础产品查路由列表和根据api查路由列表
	 * @Date 2019/11/26 16:08
	 * @param gatewayRoute
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.gateway.vo.GatewayRouteVO>>
	 **/
	@ApiLog("根据基础产品查路由列表和根据api查路由列表")
	@GetMapping("/listGatewayRoute")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayRoute")
	@Log(title = "查看网关路由自定义分页列表",businessType = BusinessType.SELECT)
	public R<IPage<GatewayRouteVO>> listGatewayRoute(GatewayRouteVO gatewayRoute, Query query) {
		IPage<GatewayRouteVO> pages = gatewayRouteService.listGatewayRoute(Condition.getPage(query), gatewayRoute);
		return R.data(pages);
	}
	
}
